from flask import Flask,render_template,request,redirect
from flask_sqlalchemy import SQLAlchemy
import json

import CommonUtils
from model import db,StuInfo
from MyJsonEncode import MyJsonEncode

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../stu.db'
db.init_app(app)
loginUser = None
@app.route('/')
def hello_world():  # put application's code here
    return "Hello World Flask"
@app.route('/login')
def login():
    text = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'J', 'K', 'L', 'M', 'N',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    verifyCode = CommonUtils.getByLengthRomdom(text,6)
    base64_str = CommonUtils.getCaptchaImageBase64ByStr(verifyCode)
    return render_template("index/login.html",base64_str = base64_str,verifyCode=verifyCode)

@app.route('/getCaptchaImage')
def getCaptchaImage():
    text = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'J', 'K', 'L', 'M', 'N',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    verifyCode = CommonUtils.getByLengthRomdom(text, 6)
    base64_str = CommonUtils.getCaptchaImageBase64ByStr(verifyCode)
    return base64_str
@app.route('/logout',methods=['post'])
def logout():
    global loginUser
    loginUser = None
    return {"code":200,"data":None,"msg":"退出成功"}

@app.route('/index')
def index():
    if loginUser is None:
        return redirect("/login")
    else:
        return render_template("index/index.html",currentUser = loginUser)
@app.route('/welcome')
def welcome():
    return render_template("index/welcome.html")

@app.route('/getAdminMenu')
def getAdminMenu():
    return render_template("index/menu1.html")
@app.route('/getFrontMenu')
def getFrontMenu():
    return render_template("index/menu2.html")

@app.route('/articleList')
def articleList():
    return render_template("index/article-list.html")
@app.route('/danyeList')
def danyeList():
    return render_template("index/danye-list.html")
@app.route('/stuInfoList')
def stuInfoList():
    return render_template("index/stuInfo-list.html")


@app.route('/stuInfo')
def stuInfo():
    sid = request.args.get('sid')
    if sid is None:
        return render_template("index/stuInfo-edit.html",userInfo = StuInfo())
    else:
        stu = StuInfo.query.filter_by(sid=sid).first()
        return render_template("index/stuInfo-edit.html",userInfo = stu)
@app.route('/personInfo')
def personInfo():
    sid = request.args.get('sid')
    stu = StuInfo.query.filter_by(sid=sid).first()
    return render_template("index/admin-info.html",userInfo = stu)

@app.route('/updateUserInfo',methods=['post'])
def updateUserInfo():
    sid = request.form.get("sid")
    name = request.form.get("name")
    email = request.form.get("email")
    desc = request.form.get("desc")
    update = StuInfo.query.get(sid)
    update.name = name
    update.email = email
    update.desc = desc
    db.session.commit()
    db.session.close()
    result = {'code': 200, 'data': None, 'msg': None}
    if update:
        result['code'] = 200
        result['msg'] = "修改成功"
    else:
        result['code'] = 201
        result['msg']="修改失败"
    return result
@app.route('/tologin',methods=['post'])
def tologin():
    global loginUser
    username = request.form.get("username")
    password = request.form.get("password")
    print(username,password)
    stu = StuInfo.query.filter_by(sid=username,password=password).first()
    result = {'code':200,'data':None,'msg':None}
    if stu:
        result['code'] = 200
        result['msg'] = "登录成功"
        loginUser = stu
    else:
        result['code'] = 201
        result['msg']="用户名或密码错误！"
    return result

@app.route('/getStuInfosLimit',methods=['post','get'])
def getStuInfosLimit():
    page = request.args.get("page",type=int)
    limit = request.args.get('limit',type=int)
    pages_data = StuInfo.query.paginate(page=page,per_page=limit)
    print(pages_data)
    result = {'code': 0, 'data': None, 'msg': None}
    result['data'] = pages_data.items
    result['count'] = pages_data.total
    result = json.dumps(result, cls=MyJsonEncode)
    print(result)
    return result
if __name__ == '__main__':
    app.run()
